package com.zbkj.crmeb.store.controller;


import com.common.CommonResult;
import com.common.PageParamRequest;
import com.zbkj.crmeb.store.model.Shop;
import com.zbkj.crmeb.store.request.ShopRequest;
import com.zbkj.crmeb.store.response.ShopResponse;
import com.zbkj.crmeb.store.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("api/admin/store/shop")
@Api(tags = "店铺") //配合swagger使用
public class ShopController {

    @Resource
    private ShopService shopService;

    @ApiOperation(value = "新增")
    @PostMapping(value = "/add")
    public CommonResult<String> save(@RequestBody ShopRequest shopRequest) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopRequest, shop);
        shopService.save(shop);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public CommonResult<String> update(@RequestBody ShopRequest shopRequest) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopRequest, shop);
        shopService.updateById(shop);
        return CommonResult.success();
    }

    @ApiOperation(value = "更新状态")
    @PostMapping(value = "/updateStatus")
    public CommonResult<String> updateStatus(@RequestBody ShopRequest shopRequest) {
        Shop shop = shopService.getById(shopRequest.getId());
        if (shop != null) {
            shop.setStatus(shopRequest.getStatus());
            shopService.updateById(shop);
        }
        return CommonResult.success();
    }

    @ApiOperation(value = "删除")
    @GetMapping(value = "/delete")
    public CommonResult<String> delete(@RequestParam Integer id) {
        Shop shop = shopService.getById(id);
        if (shop != null) {
            shop.setIsDel(1);
            shopService.updateById(shop);
        }
        return CommonResult.success();
    }

    @ApiOperation(value = "详情")
    @GetMapping(value = "/detail")
    public CommonResult<ShopResponse> detail(@RequestParam Integer id) {
        ShopResponse shopResponse = new ShopResponse();
        Shop shop = shopService.getById(id);
        BeanUtils.copyProperties(shop, shopResponse);
        return CommonResult.success(shopResponse);
    }

    @ApiOperation(value = "门店信息列表") //配合swagger使用
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<ShopResponse>> getList(ShopRequest request, PageParamRequest pageParamRequest) {
        List<ShopResponse> rspList = shopService.findAll(request)
            .stream().map(m -> {
                ShopResponse rsp = new ShopResponse();
                rsp.setId(m.getId());
                rsp.setAddress(m.getAddress());
                rsp.setName(m.getName());
                rsp.setShopNo(m.getShopNo());
                rsp.setLogo(m.getLogo());
                rsp.setImageUrl(m.getImageUrl());
                rsp.setManagerName(m.getManagerName());
                rsp.setManagerPhone(m.getManagerPhone());
                rsp.setTelephone(m.getTelephone());
                rsp.setDayTime(m.getDayTime());
                rsp.setStatus(m.getStatus());
                rsp.setIsDel(m.getIsDel());
                return rsp;
            }).collect(Collectors.toList());
        return CommonResult.success(rspList);
    }
}
